/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.xgit.iov.common.core.entity;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.text.StringEscapeUtils;

import javax.validation.constraints.NotBlank;

/**
 * 数据权限Entity
 * @author william
 * @version 2020/5/28
 */

@Data
@TableName(value = "sys_datarule")
@EqualsAndHashCode(callSuper = true)
public class SysDataRule extends Model<SysDataRule> {

	private static final long serialVersionUID = 1L;

	@TableId(type = IdType.ASSIGN_ID)
	@ApiModelProperty(value = "数据权限Id")
	@JsonSerialize(using= ToStringSerializer.class)
	private Long id;

	@ApiModelProperty(value = "所属菜单Id")
	@JsonSerialize(using= ToStringSerializer.class)
	private Long menuId;		// 所属菜单

	@NotBlank(message = "数据规则名称不能为空")
	@ApiModelProperty(value = "数据规则名称")
	private String name;		// 数据规则名称

	@NotBlank(message = "实体类名不能为空")
	@ApiModelProperty(value = "实体类名")
	private String className;   //实体类名

	@ApiModelProperty(value = "规则字段")
	private String field;		// 规则字段

	@ApiModelProperty(value = "规则条件")
	private String express;		// 规则条件

	@ApiModelProperty(value = "规则值")
	private String value;		// 规则值

	@ApiModelProperty(value = "自定义sql")
	private String sqlSegment;		// 自定义sql

	/**
	 * 备注信息
	 */
	@ApiModelProperty (value = "备注信息")
	private String remarks;

	/**
	 * 删除标识（0-正常,1-删除）
	 */
	@TableLogic
	private String delFlag;

	public String getDataScopeSql(){
		StringBuffer sqlBuffer = new StringBuffer();
		if(StrUtil.isNotBlank(field)&&StrUtil.isNotBlank(value)){
			sqlBuffer.append(" AND " +field+" "+ StringEscapeUtils.unescapeHtml4(express)+" "+value+" ");
		}
		if(StrUtil.isNotBlank(sqlSegment)){
			sqlBuffer.append(" AND "+ StringEscapeUtils.unescapeHtml4(sqlSegment)+" ");
		}

		return sqlBuffer.toString();
	}


}
